// ReportTempletQueryHandler.cpp: implementation of the ReportTempletQueryHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "ReportTempletQueryHandler.h"
#include "ReportTempletSet.h"
#include "DatabaseManager.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ReportTempletQueryHandler::ReportTempletQueryHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

ReportTempletQueryHandler::~ReportTempletQueryHandler()
{
}

int ReportTempletQueryHandler::Handling()
{
	ReportTempletQueryStruct * pReportTempletQueryStruct = (ReportTempletQueryStruct *)m_pPacket->GetpData();
	ReportTempletAckStruct * pReportTempletAckStruct = QueryReportTemplet(pReportTempletQueryStruct->m_szUserId);

	Packet packetAck(pReportTempletAckStruct->m_dwBufSize, (BYTE *)pReportTempletAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pReportTempletAckStruct;

	return 0;
}

ReportTempletAckStruct * ReportTempletQueryHandler::QueryReportTemplet(const CString& strUserId)
{
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CReportTempletSet rsReportTempletSet(pDatabase);

	rsReportTempletSet.m_strFilter.Format("UserId = '%s' OR ShareFlag = 1", strUserId);
	rsReportTempletSet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL, CRecordset::readOnly);
	std::vector<ReportTempletItem *> ReportTempletItems;
	while (!rsReportTempletSet.IsEOF())
	{
		ReportTempletItem * pReportTempletItem = new ReportTempletItem;
		strcpy(pReportTempletItem->m_szUserId, rsReportTempletSet.m_UserId);
		strcpy(pReportTempletItem->m_szTempletName, rsReportTempletSet.m_TempletName);
		strcpy(pReportTempletItem->m_szCreateDate, rsReportTempletSet.m_CreateDate);
		strcpy(pReportTempletItem->m_szCreateTime, rsReportTempletSet.m_CreateTime);
		pReportTempletItem->m_nShareFlag = rsReportTempletSet.m_ShareFlag;

		ReportTempletItems.push_back(pReportTempletItem);

		rsReportTempletSet.MoveNext();
	}
	rsReportTempletSet.Close();

	int nCount = ReportTempletItems.size();
	int nBufSize = sizeof(ReportTempletAckStruct)+sizeof(ReportTempletItem)*(nCount-1);
	ReportTempletAckStruct * pReportTempletAckStruct = (ReportTempletAckStruct*)new BYTE[nBufSize];
	pReportTempletAckStruct->m_dwMethodType = VCS_MT_QUERYREPORTTEMPLET;
	pReportTempletAckStruct->m_nMethodAck = VCS_MR_OK;
	pReportTempletAckStruct->m_dwNumOfItems = nCount;
	pReportTempletAckStruct->m_dwBufSize = nBufSize;
	for (int i = 0;i < nCount;i++)
	{
		ReportTempletItem * pReportTempletItem = ReportTempletItems[i];
		memcpy(&pReportTempletAckStruct->m_ReportTempletItems[i], pReportTempletItem, sizeof(ReportTempletItem));
		delete pReportTempletItem;
	}
	ReportTempletItems.clear();

	return pReportTempletAckStruct;
}